//
//  ShenQingKaiDianViewController.m
//  1243
//
//  Created by ym on 16/5/31.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShenQingKaiDianViewController.h"
#import "ShenQingKaiDianSecViewController.h"
#import <Photos/Photos.h>
@interface ShenQingKaiDianViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UITextFieldDelegate>
{
    NSString *judgeBtn;
}
@property (weak, nonatomic) IBOutlet UITextField *CompName;

@property (weak, nonatomic) IBOutlet UITextField *ComeUserName;

@property (weak, nonatomic) IBOutlet UITextField *IdNumber;

@property (weak, nonatomic) IBOutlet UIButton *ZhengJianZhao1;

@property (weak, nonatomic) IBOutlet UIButton *ZhengjianZhao2;

@property (weak, nonatomic) IBOutlet UITextField *YingYeZhiZhaoHao;


@property (weak, nonatomic) IBOutlet UIButton *ZhiZhaoTu1;

@property (weak, nonatomic) IBOutlet UIButton *ZhiZhaoTu2;



@property (weak, nonatomic) IBOutlet UITextField *YingYeAddress;

@property (weak, nonatomic) IBOutlet UITextField *GongSiFanWei;
@property (weak, nonatomic) IBOutlet UITextField *KaiHuMingCheng;

@property (weak, nonatomic) IBOutlet UITextField *KaiHuZhangHao;

@property (nonatomic,strong)NSMutableDictionary *ZhengJianDic;
@end

@implementation ShenQingKaiDianViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
     self.navigationController.navigationBar.translucent = NO;
    self.title = @"申请开店";
    self.ZhengJianDic = [NSMutableDictionary dictionary];
    
    self.CompName.delegate = self;
    self.ComeUserName.delegate = self;
    self.IdNumber.delegate = self;
    self.YingYeZhiZhaoHao.delegate = self;
    self.YingYeAddress.delegate = self;
    self.GongSiFanWei.delegate = self;
    self.KaiHuZhangHao.delegate = self;
    self.KaiHuMingCheng.delegate = self;
    
    
}

- (IBAction)ZhengJianTu1:(id)sender {
    // 上传证件图1
    judgeBtn = @"证件图1";
    [self getImageFromIpc];
    
    
}
- (IBAction)ZhengJianTu2:(id)sender {
    // 上传证件图1
    judgeBtn = @"证件图2";
    [self getImageFromIpc];
    
}
- (IBAction)ZhiZhaoTu1:(id)sender {
    // 上传执照图1
    judgeBtn = @"执照图1";
    [self getImageFromIpc];

}

- (IBAction)ZhiZhaoTu2:(id)sender {
    // 上传执照图2
    judgeBtn = @"执照图2";
    [self getImageFromIpc];
}



- (void)getImageFromIpc
{
    // 1.判断相册是否可以打开
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 4.设置代理
    ipc.delegate = self;
    // 5.modal出这个控制器
    [self presentViewController:ipc animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 销毁控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
 
    // 转码
    NSData *data = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 1.0f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

    
    if (encodedImageStr == nil || [encodedImageStr isEqualToString:@"" ]) {
        
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"图片添加失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alview show];
    }else{
        [_ZhengJianDic setObject:encodedImageStr forKey:judgeBtn];
        
        // 填充按钮
        if ([judgeBtn isEqualToString:@"证件图1"]) {
            [self.ZhengJianZhao1 setBackgroundImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
        }else if ([judgeBtn isEqualToString:@"证件图2"]){
            [self.ZhengjianZhao2 setBackgroundImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
        }else if ([judgeBtn isEqualToString:@"执照图1"]){
            [self.ZhiZhaoTu1 setBackgroundImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
        }else if ([judgeBtn isEqualToString:@"执照图2"]){
            [self.ZhiZhaoTu2 setBackgroundImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
        }

        
    }
    
    
}


- (IBAction)NextBtn:(id)sender {
    
   
    
    // 营业执照
    NSArray *ByteFileStringBusinessLicensePic = [[NSArray alloc] initWithObjects:_ZhengJianDic[@"执照图1"],_ZhengJianDic[@"执照图2"],nil];
    
    // 证件照
      NSArray *ByteFileStringContactCarPic = [[NSArray alloc] initWithObjects:_ZhengJianDic[@"证件图1"],_ZhengJianDic[@"证件图2"],nil];
    //
    if (self.CompName.text.length == 0 ||self.ComeUserName.text.length ==0|| self.IdNumber.text.length==0||self.YingYeZhiZhaoHao.text.length == 0 || self.YingYeAddress.text.length == 0 || self.GongSiFanWei.text.length== 0 || self.KaiHuMingCheng.text.length == 0 || self.KaiHuZhangHao.text.length == 0 || ByteFileStringContactCarPic.count != 2 || ByteFileStringBusinessLicensePic.count != 2) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alview show];

        
    }else{
        NSDictionary *dic = @{
                              @"CompanyName" : self.CompName.text,
                              @"LegalPerson":self.ComeUserName.text,
                              @"LegalPersonCardId":self.IdNumber.text,
                              @"BusinessLicense" :self.YingYeZhiZhaoHao.text,
                              @"BusinessLicenseAdress": self.YingYeAddress.text,
                              @"Business":self.GongSiFanWei.text,
                              @"CompanAccountName":self.KaiHuMingCheng.text,
                              @"CompanAccount":self.KaiHuZhangHao.text,
                              @"ByteFileStringLegalPersonPhoto":ByteFileStringContactCarPic,//证件图
                              @"ByteFileStringBusinessLicensePic":ByteFileStringBusinessLicensePic//执照图
                              
                              
                              };
        
        
        
        
        // 跳转
        ShenQingKaiDianSecViewController *a = [[ShenQingKaiDianSecViewController alloc] initWithNibName:@"ShenQingKaiDianSecViewController" bundle:[NSBundle mainBundle]];
        a.dic = dic;
        [self.navigationController pushViewController:a animated:YES];

        
        
    }
    
   }

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 1237891|| textField.tag == 1237892|| textField.tag == 1237893|| textField.tag == 1237894|| textField.tag == 1237895|| textField.tag == 1237896|| textField.tag == 1237897) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 80 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
    }
}
- (void)registerLogInTextField {
       [self.CompName resignFirstResponder];
    [self.ComeUserName resignFirstResponder];
    [self.IdNumber resignFirstResponder];
     [self.YingYeAddress resignFirstResponder];
     [self.YingYeZhiZhaoHao resignFirstResponder];
     [self.GongSiFanWei resignFirstResponder];
    [self.KaiHuMingCheng resignFirstResponder];
    [self.KaiHuZhangHao resignFirstResponder];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self registerLogInTextField];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
    //    NSLog(@"%@", textField.text);
}



@end
